using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;
using System.Linq;

namespace IQIGame.Onigao.GamePlay.Data
{
    public class UD_PortableItem : UIBaseData
    {
        public ItemData defaultItemData { get; private set; }
        public IReadOnlyList<ItemData> portableItems => _portableItems;

        private List<ItemData> _portableItems = new();

        public void UpdatePortableItems(long itemId)
        {
            _portableItems.Clear();
            defaultItemData = null;
            var cfgQuickBarItems = TableCenter.quickBarItem.DataList;
            for (var i = 0; i < cfgQuickBarItems.Count; i++)
            {
                var itemData = ItemModule.Instance.GetItemOneByCid(cfgQuickBarItems[i].Id);
                if (itemData is not { num: > 0 })
                {
                    continue;
                }
                _portableItems.Add(itemData);
                if (itemData.id == itemId)
                {
                    defaultItemData = itemData;
                }
            }

            if (defaultItemData == null)
            {
                defaultItemData = _portableItems.FirstOrDefault();
            }
        }

        public override void OnReset()
        {
            _portableItems.Clear();
            defaultItemData = null;
        }

        public static UD_PortableItem Create()
        {
            return ClassPool<UD_PortableItem>.Get();
        }
    }
}